requireJS(['constant', 'site', 'handlebars'], function (Constant, site, Handlebars) {
    var url = {
        myDataStatistic:"manager/myDataStatistic.do",
        selectSecondDepartment: "manager/selectSecondDepartment.do",
        exportMyDataStatistic:"manager/exportMyDataStatistic.do"
    };

    $(".datepicker").datepicker({
        format: 'yyyy-mm-dd',
        autoclose: true,
        pickerPosition: 'bottom',
    });

    //初始化分公司
    site.send({
        url: url.selectSecondDepartment,
        callback: function (data) {
            data = JSON.parse(data);
            if (data.type == '1') {
                var source = $("#department_list_tpl").html();
                var template = Handlebars.compile(source);
                var html = template(data.content);
                $('#department_list').append(html);
            } else {
                Constant.fn.Message('F', data.msg);
            }
        }
    })

    //查出全部数据
    function myData(timeType, timeVal, year, startTime, endTime, d2Id) {
        var data = {

        }
        if(startTime){
            data.startTime = startTime;
        }

        if(endTime){
            data.endTime = endTime;
        }

        if(d2Id){
            data.d2Id = d2Id;
        }

        if (year) {
            data.timeType = 4;
            data.year = year;
        }

        //查出客户数据
        site.send({
            url: url.myDataStatistic,
            data: data,
            callback: function (data) {
                data = JSON.parse(data)
                if (data.type == '1') {
                    //data.content.timeVal = timeVal ? timeVal : '本季度';
                    var source = $("#myData_list_tpl").html();
                    var template = Handlebars.compile(source);
                    var html = template(data.content);
                    $('#myData_list').html(html);
                }
            }
        })

    }


    //初始化客户数据
    myData();

    /*//今日，本月，本季度切换
    $('.client-pub > button').on('click', function () {
        $('.client-pub > button').addClass('btn-trans').removeClass('btn-blue')
        $(this).addClass('btn-blue').removeClass('btn-trans');
    })*/

    //搜索
    $('.sousuo').on('click', function () {
        myData($('.btn-blue').data('time'), $('.btn-blue').text(), $("#year_list").val(), $('#startTime').val(), $('#endTime').val(), $('#department_list').val());
    })

    /*//年份选择，去除前面今日，本月，本季度的选中事件
    $('#year_list').on("change",function(){
        if($(this).val()){
            $('.client-pub > button').removeClass('btn-blue').addClass('btn-trans');
        }else{
            $('.client-pub > button:eq(2)').removeClass('btn-trans').addClass('btn-blue');
        }
    })*/

    //数据导出
    $("#export").on('click',function(){
        var data = {
            //timeType:  $('.client-pub .btn-blue').data('time') ? $('.client-pub .btn-blue').data('time') : 4
        }
        if($("#year_list").val() && data.timeType === 4){
            data.year = $("#year_list").val()
        }
        if($("#department_list").val()){
            data.d2Id = $("#department_list").val()
        }
        if($("#startTime").val()){
            data.startTime = $("#startTime").val()
        }
        if($("#endTime").val()){
            data.endTime = $("#endTime").val()
        }
        site.send({
            url: url.exportMyDataStatistic,
            data: data,
            callback: function (data) {
                data = JSON.parse(data);
                if (data.type == '1') {
                    //clientOd(data.content)
                    window.open(data.content);
                }
            }
        })
    })
})